package com.hxy.apartment.dao.impl;

import com.baidu.aip.face.AipFace;
import com.hxy.apartment.dao.FaceDao;
import com.hxy.apartment.entities.Resident;
import org.json.JSONObject;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class FaceDaoImpl implements FaceDao {
    public static final String APP_ID = "22876391";
    public static final String API_KEY = "7TbrVq3fFXbU7LTMoWVmQHLq";
    public static final String SECRET_KEY = "48XksOzr5Ukow42AktlkGFvLaWVuyiMS";

    @Override
    public JSONObject register(String image, Resident resident) {
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        HashMap<String, String> options = new HashMap<>();
        options.put("user_info", "user's info");
        options.put("quality_control", "NONE");
        options.put("liveness_control", "NONE");
        options.put("action_type", "REPLACE");

        String imageType = "BASE64";
        String groupId = "group1";
        String userId = resident.getId().toString();

        // 人脸注册
        JSONObject res = client.addUser(image, imageType, groupId, userId, options);
        System.out.println(res.toString(2));
        return res;
    }

    @Override
    public JSONObject check(String image) {
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("match_threshold", "80");
        options.put("quality_control", "NONE");
        options.put("liveness_control", "NONE");
        options.put("group_id_list", "group1");

        String imageType = "BASE64";
        String groupIdList = "3,2";

        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupIdList, options);
        System.out.println(res.toString(2));
        return res;
    }
}
